package com.srlg.common.handler;

import cn.hutool.json.JSONUtil;
import com.srlg.common.req.R;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @desc:
 * @author: srlg li
 * @date: 2023/11/27 23:10
 */
@ControllerAdvice(basePackages = "com.srlg.controller")
public class RHandler implements ResponseBodyAdvice<Object> {

  @Override
  public boolean supports(MethodParameter methodParameter, Class aClass) {
    return true;
  }

  @Override
  public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
    if (o instanceof String) {
      return JSONUtil.toJsonStr(R.success(o));
    }
    return R.success(o);
  }
}
